﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 美妆产品管理系统.ruku
{
    public partial class shrk : Form
    {
        public shrk()
        {
            InitializeComponent();
        }
        SqlDataAdapter dashrk;
        DataSet ds = new DataSet();
        void init()
        {
            DB.GetCn();
            string str = "select * from rk";
            dashrk = new SqlDataAdapter(str, DB.cn);
            dashrk.Fill(ds, "shrk_info");
            DB.cn.Close();
        }
        void showAll()
        {
            init();
            showXz();
        }
        void showXz()
        {
            DataGridViewCheckBoxColumn acCode = new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode";
            acCode.HeaderText = "选择";
            dataGridView1.Columns.Add(acCode);
        }
        private void shrk_Load(object sender, EventArgs e)
        {
            showAll();
            DataView dvshrk = new DataView(ds.Tables["shrk_info"]);
            dataGridView1.DataSource = dvshrk;
        }

        private void btnsh_Click(object sender, EventArgs e)
        {
            int s = dataGridView1.Rows.Count;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {

                if (dataGridView1.Rows[i].Cells["acCode"].EditedFormattedValue.ToString() == "True")
                {
                    dataGridView1.Rows[i].Cells["ostatus"].Value = "已审核";
                    if (string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["employeeid"].Value?.ToString()))
                    {
                        dataGridView1.Rows[i].Cells["employeeid"].Value = Login.Employee_ID;
                    }
                    UpdateDB();
                }
                else
                {
                    s = s - 1;
                }

            }
            if (s == 0)
            {
                MessageBox.Show("请选择需要审核的采购单");
            }
        }

        private void btnqs_Click(object sender, EventArgs e)
        {
            int s = dataGridView1.Rows.Count;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {

                if (dataGridView1.Rows[i].Cells["acCode"].EditedFormattedValue.ToString() == "True")
                {
                    dataGridView1.Rows[i].Cells["ostatus"].Value = "已弃审";
                    if (string.IsNullOrEmpty(dataGridView1.Rows[i].Cells["employeeid"].Value?.ToString()))
                    {
                        dataGridView1.Rows[i].Cells["employeeid"].Value = Login.Employee_ID;
                    }
                    UpdateDB();
                }
                else
                {
                    s = s - 1;
                }
            }
            if (s == 0)
            {
                MessageBox.Show("请选择需要弃审的采购单");
            }
        }
        

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell ck = dataGridView1.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;
                    if (i != e.RowIndex)
                    {
                        ck.Value = false;
                    }
                    else
                    {
                        ck.Value = true;
                    }
                }
            }
        }
        void UpdateDB()//更新数据源
        {
            try
            {
                SqlCommandBuilder dbOrder = new SqlCommandBuilder(dashrk);
                dashrk.Update(ds, "shrk_info");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    }
}
